package cn.xopencode.appletweb.controller.user;


import cn.xopencode.common.framework.vo.CommonResult;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.userservice.rpc.matter.MatterRpc;
import cn.xopencode.userservice.rpc.matter.dto.CreateMatterDTO;
import cn.xopencode.userservice.rpc.matter.dto.MatterDTO;
import cn.xopencode.userservice.rpc.matter.dto.MatterPageDTO;
import cn.xopencode.userservice.rpc.matter.dto.UpdateMatterDTO;
import cn.xopencode.web.core.util.CommonWebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Api(tags = "年度重大事项说明信息管理")
@RestController
@RequestMapping("/matter")
@Validated
public class MatterController {

    @Autowired
    private HttpServletRequest request;

    @DubboReference(version = "${dubbo.consumer.MatterRpc.version}")
    private MatterRpc matterRpc;

    @ApiOperation(value = "查询单个年度重大事项说明信息")
    @GetMapping("/get/{matterId}")
    public CommonResult<MatterDTO> getDept(@RequestBody @PathVariable("matterId") String matterId) {
        return matterRpc.getMatter(matterId);
    }

    @ApiOperation(value = "年度重大事项说明列表分页查询")
    @PostMapping("/list")
    public CommonResult<PageResult<MatterDTO>> findDeptList(@RequestBody MatterPageDTO matterPageDTO) {
        return matterRpc.findMatterList(matterPageDTO);
    }

    @ApiOperation(value = "新建年度重大事项说明")
    @PostMapping("/create")
    public CommonResult<String> createDept(@RequestBody CreateMatterDTO createMatterDTO) {
        createMatterDTO.setUserId(CommonWebUtil.getUserId(request));
        return matterRpc.createMatter(createMatterDTO);
    }


    @ApiOperation(value = "批量更新年度重大事项说明信息")
    @PostMapping("/update")
    public CommonResult<Integer> updateDept(@RequestBody List<UpdateMatterDTO> updateMatterDTOList) {
        String userId = CommonWebUtil.getUserId(request);
        updateMatterDTOList.forEach(updateMatterDTO -> {
            updateMatterDTO.setUserId(userId);
        });
        return matterRpc.updateMatter(updateMatterDTOList);
    }

}
